package cn.liccshop.core.service.shopCart.controller;


import cn.liccshop.core.entity.ShopCart;
import cn.liccshop.core.service.shopCart.service.IShopCartService;
import cn.liccshop.core.utils.ObjectToOtherUtils;
import cn.liccshop.sys.constants.Constants;
import cn.liccshop.sys.entity.Result;
import cn.liccshop.sys.entity.UserElement;
import cn.liccshop.sys.exception.MySystemException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import cn.liccshop.sys.base.MyActionController;

import java.util.List;
import java.util.Map;

/**
 * @description  前端控制器
 *
 * @author LiCC
 * @since 2019-04-30
 */
@RestController
@RequestMapping("/shopCart")
public class ShopCartController extends MyActionController<ShopCart> {

    @Autowired
    private IShopCartService liShopCartService;

    @Override
    protected IService<ShopCart> getLiService(){
        return liShopCartService;
    }

    /**
     * 获取用户的购物车信息
     * @return
     * @throws MySystemException
     */
    @RequestMapping(value = "/infos")
    public Result selectShopCartInfosPro() throws MySystemException {
        UserElement userElement = getUserInfoByToken();
        /** 获取用户ID **/
        String userId = userElement.getUserId();
        if (StringUtils.isBlank(userId)){
            /** 用户ID为空 参数异常 **/
            throw new MySystemException("获取信息失败", Constants.INVALID_PARM);
        }
        List<Map<String, Object>> data = liShopCartService.selectShopCartInfos(userId);
        return new Result().setCode(Constants.OK).setData(data).setMessage("操作成功");
    }
    /**
     * 获取用户的购物车信息
     * @return
     * @throws MySystemException
     */
    @RequestMapping(value = "/infosAndOrder")
    public Result getShopCartInfosAndOrderPro(@RequestBody List<String> ids) throws MySystemException {
        UserElement userElement = getUserInfoByToken();
        /** 获取用户ID **/
        String userId = userElement.getUserId();
        if (StringUtils.isBlank(userId)){
            /** 用户ID为空 参数异常 **/
            throw new MySystemException("获取信息失败", Constants.INVALID_PARM);
        }
        List<Map<String, Object>> data = liShopCartService.getShopCartInfosAndOrderPro(userId, ids);
        return new Result().setCode(Constants.OK).setData(data).setMessage("操作成功");
    }

    /**
     * 前端保存购物车信息
     * @param entity
     * @return
     * @throws MySystemException
     */
    @RequestMapping(value = "/insert")
    public Result carInsert(@RequestBody ShopCart entity) throws MySystemException {
        UserElement userElement = getUserInfoByToken();
        /** 获取用户ID **/
        String userId = userElement.getUserId();
        if (StringUtils.isBlank(userId)){
            /** 用户ID为空 参数异常 **/
            throw new MySystemException("获取信息失败", Constants.INVALID_PARM);
        }
        entity.setUserId(userId);
        ShopCart shopCart = liShopCartService.getOne(new QueryWrapper<>(new ShopCart().setUserId(userId).setGoodsId(entity.getGoodsId()).setProductsId(entity.getProductsId())));
        if (shopCart!=null&&StringUtils.isNotBlank(shopCart.getId())){
            /** 该用户已加入了同尺寸的商品 **/
            shopCart.setCartNum(shopCart.getCartNum()+entity.getCartNum());
            liShopCartService.updateById(shopCart);
        } else {
            liShopCartService.save(entity);
        }
        return new Result().setCode(Constants.OK).setMessage("保存成功");
    }
}

